// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function () {
    // 激活当前页面的导航项
    highlightCurrentNavItem();

    // 初始化工具提示
    initTooltips();

    // 初始化轮播图自动播放
    initCarousel();
});

// 高亮当前页面的导航项
function highlightCurrentNavItem() {
    const currentPage = window.location.pathname.split('/').pop() || 'index.html';
    const navItems = document.querySelectorAll('.nav-link');

    navItems.forEach(item => {
        const itemHref = item.getAttribute('href');
        if (itemHref === currentPage ||
            (currentPage === '' && itemHref === 'index.html')) {
            item.classList.add('active');
        } else {
            item.classList.remove('active');
        }
    });
}

// 初始化工具提示
function initTooltips() {
    const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
    tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl);
    });
}

// 初始化轮播图自动播放
function initCarousel() {
    const carousels = document.querySelectorAll('.carousel');
    carousels.forEach(carousel => {
        new bootstrap.Carousel(carousel, {
            interval: 5000,
            pause: 'hover',
            wrap: true
        });
    });
}

// 产品图片缩略图点击事件
function setupProductThumbnails() {
    const thumbs = document.querySelectorAll('.product-gallery-thumb');
    const mainImage = document.querySelector('.product-main-image');

    if (thumbs.length && mainImage) {
        thumbs.forEach(thumb => {
            thumb.addEventListener('click', function () {
                // 移除所有缩略图的active类
                thumbs.forEach(t => t.classList.remove('active'));

                // 为当前点击的缩略图添加active类
                this.classList.add('active');

                // 更新主图
                mainImage.src = this.src.replace('-thumb', '');
            });
        });
    }
}

// 表单验证
function setupFormValidation() {
    const forms = document.querySelectorAll('.needs-validation');

    forms.forEach(form => {
        form.addEventListener('submit', function (event) {
            if (!form.checkValidity()) {
                event.preventDefault();
                event.stopPropagation();
            }

            form.classList.add('was-validated');
        }, false);
    });
}

// 平滑滚动
function setupSmoothScrolling() {
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function (e) {
            e.preventDefault();

            const targetId = this.getAttribute('href');
            if (targetId === '#') return;

            const targetElement = document.querySelector(targetId);
            if (targetElement) {
                targetElement.scrollIntoView({
                    behavior: 'smooth'
                });
            }
        });
    });
}

// 回到顶部按钮
function setupBackToTopButton() {
    const backToTopButton = document.createElement('button');
    backToTopButton.innerHTML = '<i class="bi bi-arrow-up"></i>';
    backToTopButton.className = 'btn btn-primary btn-back-to-top';
    backToTopButton.style.position = 'fixed';
    backToTopButton.style.bottom = '20px';
    backToTopButton.style.right = '20px';
    backToTopButton.style.display = 'none';
    backToTopButton.style.zIndex = '99';
    document.body.appendChild(backToTopButton);

    window.addEventListener('scroll', function () {
        if (window.pageYOffset > 300) {
            backToTopButton.style.display = 'block';
        } else {
            backToTopButton.style.display = 'none';
        }
    });

    backToTopButton.addEventListener('click', function () {
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
    });
}

// 初始化所有功能
function initAll() {
    highlightCurrentNavItem();
    initTooltips();
    initCarousel();
    setupProductThumbnails();
    setupFormValidation();
    setupSmoothScrolling();
    setupBackToTopButton();
}

// 页面完全加载后初始化所有功能
window.addEventListener('load', initAll);